/*
 * @Author: 火红色祥云 809880192@qq.com
 * @Date: 2023-10-04 17:34:45
 * @LastEditors: 火红色祥云 809880192@qq.com
 * @LastEditTime: 2023-10-04 22:16:51
 * @FilePath: /imx6ull-app/26_Sqlite3/main.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include "sqlite3.h"
#include "db.h"

#define DB_TABLE_NAME	"RTE_TestTable"

int main(int argc, char const *argv[])
{
	sqlite3 *DB_testHandle=NULL;
	DB_DataRow tempData={0};
	DB_OpenDB("./DB_test.db",&DB_testHandle);

	DB_CreatDBTable(DB_testHandle,DB_TABLE_NAME);
	for (int i = 0; i < 15; i++)
	{
		tempData.RTE_ID = i;
		tempData.Value = i+1;
		tempData.Defalult_Value = i+2;
		tempData.Max_Value = i+3;
		tempData.Min_Value = i;
		tempData.Offset = i+6;
		DB_ReplaceDBData(DB_testHandle,DB_TABLE_NAME,tempData);
	}

	DB_UpdateDBData(DB_testHandle,DB_TABLE_NAME,10,65530);

	DB_SelectDBData(DB_testHandle,DB_TABLE_NAME,5,&tempData);
	printf("%d-%d-%d-%d-%d-%d\r\n",tempData.RTE_ID,tempData.Value,tempData.Defalult_Value,tempData.Max_Value,tempData.Min_Value,tempData.Offset);

	DB_DataRow dataRow[5];

	DB_SelectDBRangeData(DB_testHandle,DB_TABLE_NAME,2,5,dataRow);
	for (int j = 0; j < 5; j++)
	{
		printf("%d-%d-%d-%d-%d-%d\r\n",dataRow[j].RTE_ID,dataRow[j].Value,dataRow[j].Defalult_Value,dataRow[j].Max_Value,dataRow[j].Min_Value,dataRow[j].Offset);
	}
	
	
	DB_CloseDB(DB_testHandle);
	
	return 0;
}


#if 0
char        DBFileName[80];
char        DBTableName[20];

int main(  int argc,char* argv[] )
{
	char 		SqlStr[256];
	char*        ErrMsg;
	char 		str[64];
	int			startidx;
	int         i1, rc;
	time_t      t;
	struct tm   *p;
	sqlite3*     db;
	
	startidx = 0;
	strcpy( DBFileName, "./test.db");
	strcpy( DBTableName, "table1");

	//创建数据库文件
	rc = sqlite3_open( DBFileName, &db );

	sprintf( SqlStr, "create table %s(Name char(50), Value int, Date char(50))",
			         DBTableName );
	//创建表
	sqlite3_exec( db, SqlStr, 0, 0, &ErrMsg );

	//往表里插入10条记录
	for( i1=startidx; i1<startidx+10; i1++ )
	{
		time( &t );
		p = localtime ( &t );
		sprintf( str, "%d.%d.%d-%d:%d:%d\n", p->tm_year + 1900, p->tm_mon+1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec );
		printf("%d\n", i1+1);
		sprintf(SqlStr, "insert into %s values(\"Name%d\", %d, \"%s\")",
				         DBTableName, i1+1, i1+1, str );
		sqlite3_exec( db, SqlStr, 0, 0, &ErrMsg );
		sleep(1);
	}

	sqlite3_close( db );

	return 0;
}
#endif